package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.UserInfoProxy;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.StopWatch;
	import com.wandian.view.components.ui.ScrollBarTxt;

	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;

	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	 * 聊天室界面
	 * @author long
	 * @Create  2012-10-6 下午04:10:47
	 * @QQ 108232706
	 * @version 1.0
	 */
	public class ChatRoomMediator extends Mediator
	{
		public static const NAME:String="ChatRoomMediator";
		private var _scrollChat:ScrollBarTxt;
		private var _isSendChat:Boolean=true;
		private var userinfo:UserInfoProxy;
		private var userid:String;

		public function ChatRoomMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}

		override public function onRegister():void
		{
			userinfo=GameData.getInstance().userInfo;
			userid=userinfo.userVo.userid.toString();
			GameData.stopMcAllChild(this.mcChatRoom);
			/*var scrollBar:*=GameData.getMovieClip("ScrollBarTxt");
			this.mcChatRoom.addChild(scrollBar);
			scrollBar.x=this.mcChatRoom.chatMsg_txt.x + this.mcChatRoom.chatMsg_txt.width;
			scrollBar.y=this.mcChatRoom.chatMsg_txt.y;
			this._scrollChat=new ScrollBarTxt(this.mcChatRoom.chatMsg_txt, scrollBar);*/
			this.mcChatRoom.send_btn.addEventListener(MouseEvent.CLICK, this.onSendChatHandler);
//			this.mcChatRoom.send_btn.addEventListener(MouseEvent.CLICK, this.onTestHandler);
//			Texas.I._hotkey.registerGlobalKey(Keyboard.ENTER, this.onSendChatHandler);
//			Texas.I._mcTableView.stage.focus=this.mcChatRoom.input_txt;
		}
		var msg:String;

		private function onTestHandler(event:MouseEvent=null):void
		{
			msg=this.mcChatRoom.input_txt.text;
			this.mcChatRoom.input_txt.text="";
			msg=msg.replace(new RegExp(" ", "gi"), "");
			if (msg == "")
			{
				var data1:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "请不要发空消息", tipTitle: "温馨提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
				this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data1);
				return;
			}
			var data:Object={okNoticeName: CLZNoticeName.CHATROOMMEDIATOR_TEST_AUTOADD, tip: "开始测试10000次网络命令，确定开始", tipTitle: "测试提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
			this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
		}

		private function onSendChatHandler(event:MouseEvent=null):void
		{
			var msg:String=this.mcChatRoom.input_txt.text;
			this.mcChatRoom.input_txt.text="";
			msg=msg.replace(new RegExp(" ", "gi"), "");
			if (msg == "")
			{
				return;
			}
			if (this._isSendChat)
			{
				this._isSendChat=false;
				this.sendNotification(CLZNoticeName.SOCKETCOMMAND_REQ_SENDMESSAGE, {userid: userid, message: msg, type: 0});
//				showMsg(userinfo.userVo.userid + " " + StopWatch.getDateString(new Date()) + ":" + msg);
				CLZTiming.add(0.5, this.okSendChat);
			}
			else
			{
				this.sendNotification(CLZNoticeName.CHAT_RES, {msg: "<font color=\'#666666\'>" + GameData.tip["WAIT_CHAT_TIP"] + "<font color=\'#cccccc\'>", type: 0, mid: 0});
			}
			return;
		}

		private function showMsg(msg:String):void
		{
			msg=msg.replace(new RegExp("\n", "gi"), "");
			if (this.mcChatRoom.chatMsg_txt.numLines > 50)
			{
				this.mcChatRoom.chatMsg_txt.replaceText(0, this.mcChatRoom.chatMsg_txt.getLineOffset(30), "");
			}
			this.mcChatRoom.chatMsg_txt.htmlText=this.mcChatRoom.chatMsg_txt.htmlText + (msg + "\n");
			this.mcChatRoom.chatMsg_txt.scrollV=this.mcChatRoom.chatMsg_txt.maxScrollV;
			return;
		}

		private function okSendChat():void
		{
			this._isSendChat=true;
			CLZTiming.remove(this.okSendChat);
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.CHATROOMMEDIATOR_RES_SENDMESSAGE, CLZNoticeName.CHATROOMMEDIATOR_TEST_Send_FINISH, CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED, CLZNoticeName.CHATROOMMEDIATOR_REDIRECTTO_LOGIN, CLZNoticeName.CHATROOMMEDIATOR_TEST_AUTOADD, CLZNoticeName.CHATROOMMEDIATOR_TEST_AUTOADD_FINISH];
		}

		override public function handleNotification(notification:INotification):void
		{
			var body:Object=notification.getBody();
			switch (notification.getName())
			{
				case CLZNoticeName.CHATROOMMEDIATOR_RES_SENDMESSAGE: //服务器广播消息
				{
					this.showMsg(body.message);
					/*switch (body.type)
					{
						case 0: //广播
						{
							this.showMsg(body.message);
							break;
						}
						case 1: //讨论组
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "账号密码错误！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}
						case 2: //单聊
						{
							var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "账号密码错误！", tipTitle: "登录提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
							this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
							break;
						}

						default:
						{
							break;
						}
					}*/

					break;
				}
				case CLZNoticeName.MAINMEDIATOR_SOCKET_UNCONNECTED:
				{
					var data:Object={okNoticeName: CLZNoticeName.CHATROOMMEDIATOR_REDIRECTTO_LOGIN, tip: "网络连接错误，重新登录", tipTitle: "网络提示", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
					this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
					break;
				}
				case CLZNoticeName.CHATROOMMEDIATOR_REDIRECTTO_LOGIN:
				{
					RedirectToLogin();
					break;
				}
				case CLZNoticeName.CHATROOMMEDIATOR_TEST_AUTOADD:
				{
					testAutoAdd();
					break;
				}
				case CLZNoticeName.CHATROOMMEDIATOR_TEST_Send_FINISH:
				{
					showMsg(body.toString());
					break;
				}
				case CLZNoticeName.CHATROOMMEDIATOR_TEST_AUTOADD_FINISH:
				{
					CLZTiming.add(2, this.testAutoAddFinish);
					break;
				}
				default:
				{
					break;
				}
			}
		}

		/**
		 * 测试自动增长
		 */
		private function testAutoAdd():void
		{
			this.sendNotification(CLZNoticeName.SOCKETCOMMAND_REQ_TESTAUTOADD,msg);
		}

		private function testAutoAddFinish():void
		{
			var data:Object={okNoticeName: CLZNoticeName.TIPMEDIATOR_HIDE_WINDOWS_TIP, tip: "测试10000次请求结束", tipTitle: "测试结束", isCancel: false, okTip: "OK", cancelTip: "", isSendClose: true};
			this.sendNotification(CLZNoticeName.TIPMEDIATOR_SHOW_WINDOWS_TIP, data);
		}

		private function RedirectToLogin():void
		{
			if (Texas.I._mcTableView.contains(this.mcChatRoom))
				Texas.I._mcTableView.removeChild(this.mcChatRoom);
			this.facade.removeMediator(ChatRoomMediator.NAME);
			this.sendNotification(CLZNoticeName.LOGINCOMMAND_ENTER_LOGIN);

		}

		public function get mcChatRoom():MovieClip
		{
			return viewComponent as MovieClip;
		}
	}
}
